package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Business;
import com.pangda.pojo.Clue;
import com.pangda.pojo.SysUser;
import com.pangda.pojo.Transfer;
import com.pangda.pojo.dto.TransferDto;
import com.pangda.pojo.vo.TransferVo;
import com.pangda.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 转派管理 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/transfer")
@Api(tags = "转派管理")
public class TransferController {

    @Autowired
    private ITransferService transferService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IClueService clueService;

    @GetMapping("/list")
    public PageResult<TransferVo> list(@ApiParam("分页参数") Integer pageNum,
                                       @ApiParam("分页参数") Integer pageSize,
                                       TransferDto transferDto) {
        List<SysUser> list = sysUserService.list();
        List<Transfer> transfers = BeanUtil.copyToList(list, Transfer.class);
        transfers.forEach(transfer -> {
            LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Business::getOwner, transfer.getUserName())
                    .in(Business::getStatus, "1", "2");
            transfer.setBusinessNum((long) businessService.count(wrapper));
            LambdaQueryWrapper<Clue> clueWrapper = new LambdaQueryWrapper<>();
            clueWrapper.eq(Clue::getOwner, transfer.getUserName())
                    .in(Clue::getStatus, "1", "2");
            transfer.setClueNum((long) clueService.count(clueWrapper));
            transfer.setCreateTime(LocalDateTime.now());
        });
        transferService.remove(null);
        transferService.saveBatch(transfers);
        Object beginTime = transferDto.getParams().get("beginTime");
        Object endTime = transferDto.getParams().get("endTime");
        LambdaQueryWrapper<Transfer> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(ObjectUtil.isNotEmpty(beginTime) && ObjectUtil.isNotEmpty(endTime), Transfer::getCreateTime, beginTime, endTime);

        wrapper.like(ObjectUtil.isNotEmpty(transferDto.getUserName()), Transfer::getUserName, transferDto.getUserName());
        wrapper.like(ObjectUtil.isNotEmpty(transferDto.getPhonenumber()), Transfer::getPhonenumber, transferDto.getPhonenumber());
        wrapper.eq(ObjectUtil.isNotEmpty(transferDto.getUserId()), Transfer::getUserId, transferDto.getUserId());
        Page<Transfer> page = transferService.page(new Page<>(pageNum, pageSize), wrapper);

        return PageResult.success(page, TransferVo.class);
    }


    @PutMapping("/assignment/{type}/{userId}/{transferUserId}")
    public ResponseResult assignment(@PathVariable String type,
                                     @PathVariable Integer userId,
                                     @PathVariable Integer transferUserId) {
        return ResponseResult.success();
    }
}
